﻿
namespace Patterns4Net.Tests.ArchitectureExplorer.ViewModels.Services
{
    using System;
    using System.Linq;
    using System.Xml.Linq;
    using Patterns4Net.ArchitectureExplorer.ViewModels.Services;
    using NUnit.Framework;

    [TestFixture]
    public class VisualStudioFilesParserTest
    {
        [Test]
        public void GetAssemblyFilenameTest()
        {
            XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
            var doc = new XDocument(
                new XElement(ns + "Project",
                    new XElement(ns + "AssemblyName", "MyAssembly"),
                    new XElement(ns + "OutputPath", "bin\\Debug"),
                    new XElement(ns + "OutputPath", "bin\\Release")));

            var parser = new VisualStudioFilesParser();
            var expected = "C:\\xxx\\bin\\Debug\\MyAssembly.dll";
            var filename = parser.GetAssemblyFilename(
                "C:\\xxx\\project.csproj",
                doc,
                file => file == expected ||
                        file == "C:\\xxx\\bin\\Release\\MyAssembly.dll",
                file =>
                    file == expected
                        ? new DateTime(2010, 1, 1)
                        : new DateTime(2000, 1, 1));

            Assert.AreEqual(expected, filename);
        }
       
        [Test]
        public void GetAssemblyFilenamesFromSolutionTest()
        {
            var content = @"
                Microsoft Visual Studio Solution File, Format Version 11.00
                # Visual Studio 2010
                Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""Patterns4Net.ArchitectureExplorer"", ""Patterns4Net\ArchitectureExplorer.csproj"", ""{2F4CBA16-C07E-47A1-886B-592985615A4D}""
                EndProject
                Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""Patterns4Net.Core"", ""Patterns4Net\Core.csproj"", ""{2E9EF2C3-10E1-4425-9E2E-B876F95E6461}""
                EndProject
                Global
	                GlobalSection(SolutionConfigurationPlatforms) = preSolution
		                Contracts|Any CPU = Contracts|Any CPU
		                Contracts|Mixed Platforms = Contracts|Mixed Platforms
		                Contracts|x86 = Contracts|x86
		                Debug|Any CPU = Debug|Any CPU
		                Debug|Mixed Platforms = Debug|Mixed Platforms
		                Debug|x86 = Debug|x86";

            var parser = new VisualStudioFilesParser();
            var result = parser.GetProjectFilenames("C:\\yyy\\solution.sln", content);

            CollectionAssert.AreEquivalent(
                new[] {"C:\\yyy\\Patterns4Net\\ArchitectureExplorer.csproj", "C:\\yyy\\Patterns4Net\\Core.csproj"},
                result.ToArray());
        }
    }
}
